const path = require('path');

module.exports = {
  entry: {
    index: [path.resolve(__dirname, 'src/index.tsx')],
  },
  output: {
    path: 'dist',
    filename: '[name].[chunkhash:8].js',
    chunkFilename: '[name].[chunkhash:8].chunk.js',
  },
  module: {
    rules: [
      {
        test: /\.(js|jsx|ts|tsx)$/,
        include: /(src)/,
        exclude: /(node_modules)/,
        use: [
          {
            loader: 'babel-loader',
            options: {
              cacheDirectory: true,
            },
          },
        ],
      },
    ],
  },
  modules: ['node_modules', 'src'],
  extensions: ['.ts', '.tsx', '.js', '.jsx', '.json'],
  alias: {
    '@': path.resolve(__dirname, 'src'),
    // '@components': resolveApp('src/components'),
    // '@constants': resolveApp('src/constants'),
    // '@containers': resolveApp('src/containers'),
    // '@models': resolveApp('src/models'),
    // '@pages': resolveApp('src/pages'),
    // '@services': resolveApp('src/services'),
    // '@redux': resolveApp('src/redux/'),
    // '@utils': resolveApp('src/utils'),
  },
  watchOptions: {
    ignored: /node_modules/,
  },
  externals: {
    react: 'React',
    'react-dom': 'ReactDOM',
    redux: 'Redux',
    'react-redux': 'ReactRedux',
    'react-router-dom': 'ReactRouterDOM',
  },
  optimization: {
    chunkIds: 'named',
    minimize: true,
    minimizer: [
      new TerserPlugin({
        test: /\.js(\?.*)?$/i,
        parallel: os.cpus().length - 1,
      }),
    ],
    splitChunks: {
      chunks: 'all',
      minSize: 10000,
      minChunks: 2,
      cacheGroups: {
        defaultVendors: {
          test: /[\\/]node_modules[\\/]/,
          priority: -10,
          reuseExistingChunk: true,
        },
        default: {
          minChunks: 2,
          priority: -20,
          reuseExistingChunk: true,
        },
      },
    },
  },
  cache: {
    type: 'filesystem',
    // 额外的依赖文件，当这些文件内容发生变化时，缓存会完全失效而执行完整的编译构建，通常可设置为项目配置文件
    buildDependencies: {
      defaultWebpack: ['webpack/lib/'],
      config: [__filename],
      // name: `${process.env.NODE_ENV || 'development'}-cache`
    },
  },
};
